package com.hailou.service;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;

import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.codehaus.jackson.map.MappingJsonFactory;

public class SimsimiService {

	
	public String JSESSIONID_STR = "JSESSIONID=DF464B61E515AE38A646CF610403E21E";
	public String AWSELB_STR = "AWSELB=19EDFB13061D5522C417E7CC830CFD1AE49AA14933D92A4798D892E1C74E8285D459FA462E94FA945A3522503E16B66671372F5A0346D270A9F955361F579D83AD804F79D1";
	
	public String curl_init = "http://www.simsimi.com/func/req?lc=ch&msg=";
	public String respStr = "这个问题对我太难，请换个问题好吗？";

	
	public  String getSimsimiString(String keyword) {
		StringBuilder sb = new StringBuilder();
		URL url;
		try {
			url = new URL(curl_init+URLEncoder.encode(keyword, "utf-8"));
			HttpURLConnection huc = (HttpURLConnection) url.openConnection();
			huc.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01");
			huc.setRequestProperty("Accept-Encoding", "gzip, deflate");
			huc.setRequestProperty("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
			huc.setRequestProperty("Connection", "keep-alive");
			huc.setRequestProperty("Content-Type", "application/json; charset=utf-8");
			huc.setRequestProperty("Cookie", "sagree=true;"+AWSELB_STR+"; selected_nc=ch; "+JSESSIONID_STR);
			huc.setRequestProperty("Host", "www.simsimi.com");
			huc.setRequestProperty("Referer", "http://www.simsimi.com/talk.htm");
			huc.setRequestProperty("X-Requested-With", "XMLHttpRequest");
			
			huc.setRequestMethod("GET");
			 // 获取输入流  
            InputStream inputStream = huc.getInputStream();  
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");  
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);  
  
            // 读取返回结果   
            String str = null;  
            while ((str = bufferedReader.readLine()) != null) {  
            	sb.append(str); 
            }  
  
            // 释放资源  
            bufferedReader.close();  
            inputStreamReader.close();  
            inputStream.close();  
            huc.disconnect(); 
            
            
            JsonFactory jsonFactory = new MappingJsonFactory();  
            JsonParser jsonParser = jsonFactory.createJsonParser(sb.toString());  
            jsonParser.nextToken();  
            HashMap<String, String> map = new HashMap<String, String>();  
            while (jsonParser.nextToken() != JsonToken.END_OBJECT) {  
                jsonParser.nextToken();  
                map.put(jsonParser.getCurrentName(), jsonParser.getText());  
            }
            System.out.println(map.get("result"));
        	if("100".equals(map.get("result"))){
        		respStr = map.get("response");
        	}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return respStr;
	}
	

	//转义特殊字符
	public String changeSpecialCharacter(String str)
	{                                          
		str = str.replace("@", "%40").replace("#", "%23").replace("%", "%25")
				.replace("&", "%26").replace("(", "%28").replace(")", "%29").replace("+", "%2B")
				.replace("=", "%3D").replace("<", "%3C").replace(">", "%3E").replace("\"", "%22")
				.replace(",", "%2C").replace("/", "%2F").replace(":", "%3A").replace(";", "%3B")
				.replace("?", "%3F").replace("\\", "%5C").replace("|", "%7C");
	     return str;
	}
	

	public static void main(String[] args) {
		try {
			System.out.println(new SimsimiService().getSimsimiString(URLEncoder.encode("你好啊!", "utf-8")));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
